﻿using AntdUI;
using NetTaste;

namespace TestPressure
{
    public partial class DevicePointManageForm : BaseForm
    {

        #region

        private DevicePoint? obj;

        public DeviceItem deviceItem;

        #endregion

        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_number"] = deviceItem.deviceNumber;
            List<DevicePoint> list = SqlSugarHelper.Inquireable<DevicePoint>(where);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<DevicePoint> list)
        {
            var antItemList = new List<DevicePointItem>();
            foreach (DevicePoint item in list)
            {
                string range = item.range_min.ToString() + "-" + item.range_max.ToString();
                string outValue = item.out_min.ToString() + "-" + item.out_max.ToString();
                string alarm = item.alarm_low.ToString() + "-" + item.alarm_hight.ToString();
                string registerType = intToRegisterTypeString(item.register_type);

                antItemList.Add(new DevicePointItem(item.device_number, item.device_name, item.point_number,
                                                    item.point_name, item.point_address, range, outValue, item.amend,
                                                    alarm, item.error_range.ToString(), item.unit, registerType));
            }
            Table.DataSource = antItemList;
        }

        /// <summary>寄存器类型
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string intToRegisterTypeString(int type)
        {
            string registerType = "";
            if (type == 1)
            {
                registerType = "DI";
            }
            else if (type == 2)
            {
                registerType = "DO";
            }
            else if (type == 3)
            {
                registerType = "InputRegister";
            }
            else if (type == 4)
            {
                registerType = "HoldingRegister";
            }
            return registerType;
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckRowExist(int number)
        {
            obj = ObjectWithPrimaryKey(number);
            if (obj == null)
            {
                LoadList();
                string msg = String.Format("编号【{0}】的设备不存在", number);
                Error(msg,this);
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>根据主键查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private DevicePoint? ObjectWithPrimaryKey(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["point_number"] = number;
            List<DevicePoint> list = SqlSugarHelper.Inquireable<DevicePoint>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>根据主键查询设备
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Device? DeviceWithPrimaryKey(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_number"] = number;
            List<Device> list = SqlSugarHelper.Inquireable<Device>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        #endregion

        #region 系统方法
        public DevicePointManageForm()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterParent;
            FullScreen = true;
        }

        private void DevicePointManageForm_Load(object sender, EventArgs e)
        {
            Header.Text = "设备点位管理";
            
            Table.Height = Height - Header.Height - StackPanel1.Height;
            Table.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("device_number","设备编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("device_name","设备名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("point_number","点位编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("point_name","点位名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("point_address","点位地址",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("range","量程范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("outValue","输出范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("alarm","报警范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("error_range","判断范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("unit","数据单位",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("register_type","寄存器类型",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("operation","操作",AntdUI.ColumnAlign.Center){ Width="120", LineBreak=true}
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table.Spin(config, () =>
            {
                LoadList();
            }, () =>
            {

            });

            if (deviceItem is not null)
            {
                Device? device = DeviceWithPrimaryKey(deviceItem.deviceNumber);
                if (device == null)
                {
                    
                    if (this.Owner is not null)
                    {
                        DeviceManageForm frm = (DeviceManageForm)this.Owner;
                        frm.LoadList();
                        string msg = string.Format("【{0}】设备不存在，无法编辑点位",deviceItem.deviceName);
                        frm.Error(msg, frm);
                        this.Close();
                    }
                    
                }
            }
        }
        #endregion

        #region 响应事件
        private void AddButton_Click(object sender, EventArgs e)
        {
            DevicePointAddForm frm = new DevicePointAddForm();
            frm.Owner = this;
            frm.deviceItem = deviceItem;
            frm.ShowDialog();
        }

        /// <summary>点击表格操作按钮
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Table_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is DevicePointItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    bool res = CheckRowExist(data.point_number);
                    if (!res)
                    {
                        return;
                    }
                    string msg = string.Format("您确定要删除【{0}】设备点位吗？", data.point_name);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            if (obj is not null)
                            {
                                List<DevicePoint> list = new List<DevicePoint>();
                                list.Add(obj);
                                int deleteRes = SqlSugarHelper.Deleteable(list);
                                if (deleteRes > 0)
                                {
                                    LoadList();
                                }
                                else
                                {
                                    Error("删除设备点位失败",this);
                                }
                            }
                            else
                            {
                                Error("删除设备点位失败", this);
                            }
                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
